package com.tao.util.pack.send;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tao.domain.Group;
import com.tao.domain.SendMessage;
import com.tao.domain.User;
import com.tao.util.Utils;
import com.tao.util.pack.BaseResponeBean;
import com.tao.util.pack.Msg;
import com.tao.util.pack.SendCount;
import com.tao.util.pack.SendCountBean;
import com.tao.util.pack.send.impl.GroupMessageInterImpl;

public class SendHttpUtil extends Thread {

	boolean isBeat = false;
	// ************** 一些变量
	public List<User> userlist;
	public List<Group> grouplist;
	public Msg msg;
	public String imagePath;
	private String name  = "";
	private List<SendMessage> list   = new ArrayList<SendMessage>();
	private SendCountBean sBean = new SendCountBean(); // 发送状态
	private int sendSuccessCount = 0; // 已发送成功数
	private int sendFailCount = 0; // 已发送失败数
	private List<SendCount> listCounts = new ArrayList<SendCount>(); // 每一个群发送失败状态
	// ***************监听接口

	Gson gson = new Gson();
	/**
	 * 线程启动监听器
	 */
	private OnInitMsgListener mInitMsgListener;
	/**
	 * 发送信息监听器
	 */
	private OnSendStatsMsgListener mSendStatsMsgListener;
	/**
	 * 已发送结束监听监听器
	 */
	private OnSendStatsExitMsgListener mSendStatsExitMsgListener;

	public interface OnInitMsgListener {// 线程启动监听器
		void onInitMsg(String text);
	}

	public interface OnSendStatsMsgListener {// 发送信息监听器
		void onSendStatsMsg(String text,SendMessage json);
	}

	public interface OnSendStatsExitMsgListener {// 已发送结束监听监听器
		void onExitMsg(String text);
	}

	public SendHttpUtil(List<User> userlist, List<Group> grouplist, Msg msg,
			String imagePath, List<SendMessage> list) {
		super();
		this.userlist = userlist;
		this.grouplist = grouplist;
		this.msg = msg;
		this.imagePath = imagePath;
		this.list = list;
	}

	public SendHttpUtil() {
	}

	public void setmInitMsgListener(OnInitMsgListener mInitMsgListener) {
		this.mInitMsgListener = mInitMsgListener;
	}

	public void setmSendStatsMsgListener(
			OnSendStatsMsgListener mSendStatsMsgListener) {
		this.mSendStatsMsgListener = mSendStatsMsgListener;
	}

	public void setmSendStatsExitMsgListener(
			OnSendStatsExitMsgListener mSendStatsExitMsgListener) {
		this.mSendStatsExitMsgListener = mSendStatsExitMsgListener;
	}

	@Override
	public void run() {

		mInitMsgListener.onInitMsg("发送线程启动");

		String result = null;
		GroupMessage gMessage = new GroupMessageInterImpl();
		
		// 遍历用户
		for (User user : userlist) {
			// 遍历群组
			for (Group group : grouplist) {
				
				if (group.getUin().equals(user.getUin())) {
					msg.setToUserName(group.getGroupName());
				
					// 判断是发图片还是文本
					if (null != imagePath && !imagePath.equals("")) {
                        //发送文本
						result = gMessage.sendGroupImageMsg(imagePath, user,msg);
                      
					} else {
						 //发送图片
						result = gMessage.sendGroupMsg(user, msg);
					}
					
					
					 //图片是否已发送，没有则从新发送	
					if (null == result || result.equals("")) {
						if (null != imagePath && !imagePath.equals("")) {
						result = gMessage.sendGroupImageMsg(imagePath,user, msg);
						}else{
						result = gMessage.sendGroupMsg(user, msg);
						}
					}
					
					//文本是否已发送，没有则从新发送
					
					// 处理发送返回的状态数据
					JsonString(user, group, result);

					

					try {
						
						// 延迟10秒发信息
						sleep(3000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

		}
		// 设置返回前台页面 发送成功数 发送失败群 发送图片路径
		sBean.setSendSuccessCount("" + sendSuccessCount);
		sBean.setSendCountList(listCounts);
		sBean.setName(name);
		
		if (null != imagePath && !imagePath.equals("")) {
			sBean.setImagePath(imagePath);
		}else {
			sBean.setImagePath("\\");
		}
		
		sendSuccessCount = 0;
		listCounts = null;
		// 发送完毕
		mSendStatsExitMsgListener.onExitMsg(new Gson().toJson(sBean).toString());

	}

	// 已发送状态 ret=0 发送成功
	public void JsonString(User user, Group group, String result) {
		 SendCountBean sBeans = new SendCountBean(); // 发送状态
		int ret = new Gson().fromJson(result, BaseResponeBean.class)
				.getBaseResponse().getRet();
	
		SendMessage sMege=null;
		
		
		
			
		
		if (ret == 0) {
			//遍历提取发信息中已发送成功的记录
			
			for(SendMessage sMessage:list){
				if(sMessage.getGroupNickName().equals(group.getGroupNickName())){
					//保存数据库
					sMessage.setSendtime(Utils.getSystemTime());
					sMege = sMessage;
					break;
				}
			}
			
			name = group.getGroupNickName()+":已发送成功";
			sendSuccessCount++;
			sBeans.setSendSuccessCount("" + sendSuccessCount);
			sBeans.setName(name);
			sBean.setImagePath("\\");
			
		} else {
			name = group.getGroupNickName()+":发送失败";
			listCounts.add(new SendCount(user.getNickName(),group.getGroupNickName()));
			sBeans.setName(name);
			sBean.setImagePath("\\");
			sBeans.setSendSuccessCount("" + 0);
		}
		
		mSendStatsMsgListener.onSendStatsMsg(new Gson().toJson(sBeans).toString(),sMege);
	}

}
